package com.example.e_comm.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Vehicle;
import com.example.demo.service.VehicleService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/api/vehicle")
@Validated
public class VehicleController {
    private final VehicleService vehicleService;

    public VehicleController(VehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }
    @GetMapping("/online")
    public ResponseEntity<List<Vehicle>> getOnlineVehicles() {
        List<Vehicle> vehicles = vehicleService.getOnlineVehicles();
        return ResponseEntity.ok(vehicles);
    }
    @GetMapping("/list")
    public ResponseEntity<IPage<Vehicle>> getVehiclePage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        IPage<Vehicle> page = new Page<>(current, size);
        IPage<Vehicle> vehiclePage = vehicleService.getVehiclePage(page);
        return ResponseEntity.ok(vehiclePage);
    }
    @GetMapping("/info")
    public ResponseEntity<Vehicle> getVehicleByVin(@RequestParam String vin) {
        Vehicle vehicle = vehicleService.getVehicleByVin(vin);
        return ResponseEntity.ok(vehicle);
    }
    @PostMapping("/report")
    public ResponseEntity<Boolean> reportVehicleStatus(@Valid @RequestBody Vehicle vehicle) {
        boolean result = vehicleService.reportVehicleStatus(vehicle);
        return ResponseEntity.ok(result);
    }
}